package com.tornado.community.controller;

import com.tornado.community.advice.Constant;
import com.tornado.community.advice.ErrorCode;
import com.tornado.community.advice.NotificationTypeEnum;
import com.tornado.community.dto.CommentCreateDto;
import com.tornado.community.dto.CommentDto;
import com.tornado.community.mapper.CommentMapper;
import com.tornado.community.mapper.NotificationMapper;
import com.tornado.community.mapper.QuestionMapper;
import com.tornado.community.mapper.UserMapper;
import com.tornado.community.mapper.custome.MyQuestionMapper;
import com.tornado.community.model.Comment;
import com.tornado.community.model.Notification;
import com.tornado.community.model.User;
import com.tornado.community.service.CommentService;
import com.tornado.community.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

@Controller
public class CommentController {
    private final CommentMapper commentMapper;
    private final MyQuestionMapper myQuestionMapper;
    private final CommentService commentService;
    private final NotificationService notificationService;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    public CommentController(CommentMapper commentMapper, MyQuestionMapper myQuestionMapper,
                             NotificationService notificationService, CommentService commentService) {
        this.commentMapper = commentMapper;
        this.myQuestionMapper = myQuestionMapper;
        this.commentService = commentService;
        this.notificationService = notificationService;
    }

    @PostMapping("/comment")
    @ResponseBody
    public String comment(@RequestBody CommentCreateDto commentCreateDto,
                          HttpSession session) {
        Comment comment = new Comment();
        comment.setParentId(commentCreateDto.getParentId());
        comment.setContent(commentCreateDto.getContent());
        comment.setType(commentCreateDto.getType());
        comment.setGmtCreate(System.currentTimeMillis());
        comment.setGmtModify(comment.getGmtCreate());
        User user = Objects.requireNonNull((User) session.getAttribute("user"), ErrorCode.NO_USER_IN_SESSION);
        comment.setCommenter(user.getId());
        commentMapper.insertSelective(comment);
        //添加通知
        Notification notification = notificationService.insertNotification(comment);
        int unreadCount = notificationService.unreadCount(notification.getReceiver());
        session.setAttribute("unreadCount", unreadCount);
        //增加问题回复统计
        if (Constant.CommentType.QUESTION == commentCreateDto.getType()) {
            myQuestionMapper.incComment(Math.toIntExact(commentCreateDto.getParentId()));
        }
        return "OK";
    }

    @GetMapping("/comment/{id}")
    @ResponseBody
    public List<CommentDto> selectCommentComment(@PathVariable("id") Long id) {
        return commentService.selectCommentComment(id);
    }

}
